package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.Dao.Impl.BannerDaoImpl;
import com.ysd.Dao.Impl.DesignDaoImpl;
import com.ysd.entity.Banner;
import com.ysd.entity.Design;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminDesignServlet extends BaseServlet{
/**
 * 查询所有设计师
 * @author 86176
 * 路径adminDesign?reqName=getDesignByPage
 *
 */
	public void getDesignByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		DesignDaoImpl dDao = new DesignDaoImpl() ;
		List<Design> design = dDao.getDesignByPage((page-1)*size,size);
		
		Gson gson= new Gson();
		String res = gson.toJson(design);
		out.print(res);
		
		
		out.flush();
		out.close();
		
	
	}
	/**
	 * 查询所有设计师长度
	 * @author 86176
	 * 路径adminDesign?reqName=getDesign
	 *
	 */
		public void getDesign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			DesignDaoImpl dDao = new DesignDaoImpl() ;
			List<Design> design = dDao.getDesign();
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			Gson gson= new Gson();
			String res = gson.toJson(design.size());
			out.print(res);//响应数据
			
			
			out.flush();
			out.close();
			
		
		}
	/**
	 * 删除指定设计师
	 * localhost:8080/zhubaosheji/adminDesign?reqName=deleteDesign
	 * @author 86176
	 *
	 */
		public void deleteDesign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int dId = Integer.parseInt(request.getParameter("designId"));
			DesignDaoImpl dDao = new DesignDaoImpl() ;//创建设计师实体类
			int n = dDao.deleteDesignById(dId);//调用删除方法
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			if (n>0) {//删除成功
				out.print(Result.toClient(true, "删除成功",""));//{"success":true,"message":"删除成功","remark":""}	
			} else {//删除失败
				out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败","remark":""}	
			}
			
			out.flush();
			out.close();
		}

		/**
		 * 批量删除指定设计师
		 * localhost:8080/zhubaosheji/adminDesign?reqName=deleteDesignByIds
		 * @author 86176
		 *
		 */
			public void deleteDesignByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				String dIds = request.getParameter("designId");
				DesignDaoImpl dDao = new DesignDaoImpl() ;//创建设计师实体类
				int n = dDao.deleteDesignByIds(dIds);//调用删除方法
				
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				
				if (n>0) {//删除成功
					out.print(Result.toClient(true, "删除成功",""));//{"success":true,"message":"删除成功","remark":""}	
				} else {//删除失败
					out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败","remark":""}	
				}
				
				out.flush();
				out.close();
			}
		/**
		 * 添加指定设计师
		 * localhost:8080/zhubao/adminDesign?reqName=addDesign
		 * @author 86176
		 *
		 */
		public void addDesign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			DesignDaoImpl dDao = new DesignDaoImpl();//创建设计师实现类
			
			String designTitle =request.getParameter("picName");
			String designName =request.getParameter("designName");
			String designGrade =request.getParameter("designGrade");
			if(designTitle != null && !designTitle.equals("")&& designName!= null && !designName.equals("")&& designGrade!= null && !designGrade.equals("")) {
					Design de = new Design(0,designTitle, designName,designGrade);
					int n =dDao.addDesign(de);//调用添加设计师方法
					
					if (n>0) {//添加成功
						out.print(Result.toClient(true, "添加成功",""));//{"success":true,"message":"删除成功","remark":""}	
					} else {//添加失败
						out.print(Result.toClient(false, "添加失败",""));//{"success":false,"message":"删除失败","remark":""}	
					}
					
			}else {
				out.print(Result.toClient(false, "添加失败,图片路径或设计师名或设计师级别为空",""));//图片路径或设计师名或设计师级别为空
			}
			
			out.flush();
			out.close();
		}
		
		/**
		   *根据Id查询指定设计师回显数据
		 * localhost:8080/zhubao/adminDesign?reqName=getDesignById
		 * @author 86176
		 *
		 */
		public void getDesignById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			DesignDaoImpl dDao = new DesignDaoImpl();//创建设计师实现类
			
			int designId =Integer.parseInt(request.getParameter("dId"));//获取前台传来的参数             
			Design design = dDao.getDesignById(designId);//调用根据id查询的方法
			
			Gson gson= new Gson();//创建gson对象
			String res = gson.toJson(design);//将design转化为json字符串
			
			out.print(res);//响应数据
			System.out.println(res);
			
					
			out.flush();
			out.close();
		}
		/**
		 * 编辑指定设计师
		 * localhost:8080/zhubao/adminDesign?reqName=editDesign
		 * @author 86176
		 *
		 */
		public void editDesign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			DesignDaoImpl dDao = new DesignDaoImpl();//创建设计师实现类
			
			int designId = Integer.parseInt(request.getParameter("designId"));
			String designTitle =request.getParameter("picName");
			String designName =request.getParameter("designName");
			String designGrade =request.getParameter("designGrade");
			
			if(designTitle != null && !designTitle.equals("")&& designName!= null && !designName.equals("")&& designGrade!= null && !designGrade.equals("")) {
				Design de = new Design(designId,designTitle, designName,designGrade);
				int n =dDao.editDesign(de);//调用编辑设计师方法
				
				if (n>0) {//修改成功
					out.print(Result.toClient(true, "修改成功",""));//{"success":true,"message":"修改成功","remark":""}	
				} else {//修改失败
					out.print(Result.toClient(false, "修改失败",""));//{"success":false,"message":"修改失败","remark":""}	
				}
				
		}else {
			out.print(Result.toClient(false, "修改失败,图片路径或设计师名或设计师级别为空",""));//图片路径或设计师名或设计师级别为空
		}
		
		out.flush();
		out.close();
		}
}
